import { useEffect, useState } from 'react'
import axios from 'axios'
import { notification } from 'antd'

export default function usePublish(type) {
  const { username } = JSON.parse(localStorage.getItem('token'))

  const [dataSource, setdataSource] = useState([])

  useEffect(() => {
    getSourceData()
  }, [username,type])

  const getSourceData = () => {
    axios(
      `/news?author=${username}&publishState=${type}&_expand=category`,
    ).then((res) => {
      setdataSource(res.data)
    })
  }

  const handlePublish = (id) => {
    axios
      .patch(`/news/${id}`, {
        publishState: 2,
        publishTime: Date.now(),
      })
      .then((res) => {
        getSourceData()
        notification.info({
          message: `通知`,
          description: `您可以到【发布管理/已经发布】中查看您的新闻`,
          placement: 'bottomRight',
        })
      })
  }
  const handleSunset = (id) => {
    axios
      .patch(`/news/${id}`, {
        publishState: 3,
      })
      .then((res) => {
        getSourceData()
        notification.info({
          message: `通知`,
          description: `您可以到【发布管理/已下线】中查看您的新闻`,
          placement: 'bottomRight',
        })
      })
  }
  const handleDelete = (id) => {
    axios.delete(`/news/${id}`).then((res) => {
      getSourceData()
      notification.info({
        message: `通知`,
        description: `您已经删除了已下线的新闻`,
        placement: 'bottomRight',
      })
    })
  }

  return {
    dataSource,
    handlePublish,
    handleSunset,
    handleDelete,
  }
}
